import request from '@/utils/request';
import { ResponseData, SUCCESS_CODE } from '@/utils/types.d';
import { ComplaintTableItem, NegotiationTableItem } from './types.d';

export async function queryWechatComplaint(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ComplaintTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: ComplaintTableItem[];
  }> = await request('/api/wx-complaint/v1.0.0/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryNegotiations(
  complaintId: string,
  params: any,
): Promise<{
  success: boolean;
  total: number;
  data: NegotiationTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: NegotiationTableItem[];
  }> = await request(`/api/wx-complaint/v1.0.0/${complaintId}/negotiations`, {
    method: 'GET',
    params: {
      data: params,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function replayUser(complaintId: string, form: FormData) {
  const res = await request(`/api/wx-complaint/v1.0.0/${complaintId}/replay`, {
    method: 'POST',
    requestType: 'form',
    // body: form,
    data: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '操作成功',
    };
  return { data: res?.data, success: false, message: res?.message || '操作失败' };
}

export async function updateRefundStatus(complaintId: string, form: FormData) {
  const res = await request(`/api/wx-complaint/v1.0.0/${complaintId}/check-refund`, {
    method: 'POST',
    requestType: 'form',
    // body: form,
    data: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '操作成功',
    };
  return { data: res?.data, success: false, message: res?.message || '操作失败' };
}

export async function completeComplaint(complaintId: string) {
  const res = await request(`/api/wx-complaint/v1.0.0/${complaintId}/complete`, {
    method: 'POST',
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '操作成功',
    };
  return { data: res?.data, success: false, message: res?.message || '操作失败' };
}

export async function getComplaintDetail(complaintId: string): Promise<{
  success: boolean;
  message: string;
  data: ComplaintTableItem;
}> {
  const res: ResponseData<ComplaintTableItem> = await request(
    `/api/wx-complaint/v1.0.0/detail/${complaintId}`,
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '获取成功', data: res?.data };
  return {
    success: true,
    message: res?.message || '获取失败',
    data: res?.data,
  };
}
